package com.example.shaoying.yuejian.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.fragment.clubeFragment.ClubeJieShaoFragment;
import com.example.shaoying.yuejian.fragment.clubeFragment.ClubeShiPinFragment;
import com.example.shaoying.yuejian.fragment.clubeFragment.ClubeKeChengFragment;
import com.example.shaoying.yuejian.model.ClubeModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClubInfoActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private ClubeShiPinFragment clubVideoFragment;
    private ClubeJieShaoFragment clubIntroductionFragment;
    private ClubeKeChengFragment clubCourseFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    private TextView Club_Name;
    private ImageView Club_Pic,guanzhu;
    private String Club_Name_xml;
    private String Club_Name_Pic;
    private boolean flag;

    private SharedPreferences sp;
    private String userId;//为用户id
    private String clubId;
    private String sessionId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.clubinfo);
        intvie();
        event();

        manager=getSupportFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId){

                    case R.id.radio_club_info:
                        clubIntroductionFragment=new ClubeJieShaoFragment();
                        replace(clubIntroductionFragment);
                        break;
                    case R.id.radio_club_course:
                        clubCourseFragment=new ClubeKeChengFragment();
                        replace(clubCourseFragment);
                        break;
                    case R.id.radio_club_video:
                        clubVideoFragment=new ClubeShiPinFragment();
                        replace(clubVideoFragment);
                        break;
                }
            }
        });

        radioGroup.check(R.id.radio_club_info);

    }

    private void event() {
        guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClubeModel model = new ClubeModel();
                    if(!flag){
                    Call<String> call = model.guanzhu(sessionId, userId, clubId, "1");
                    call.enqueue(new Callback<String>() {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            String res = response.body();
                            if(Integer.parseInt(res)>0) {
                                flag=true;
                                guanzhu.setImageResource(R.drawable.ic_guanzhu_hong);
                                Toast.makeText(ClubInfoActivity.this, res, Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {

                        }

                    });
                }else{
                        Call<String> call=model.delete("clubfans", clubId, userId);
                        call.enqueue(new Callback<String>() {
                            @Override
                            public void onResponse(Call<String> call, Response<String> response) {
                                String res = response.body();
                                if(Integer.parseInt(res)>0) {
                                    flag=false;
                                    guanzhu.setImageResource(R.drawable.ic_guanzhu_bai);
                                    Toast.makeText(ClubInfoActivity.this, res, Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onFailure(Call<String> call, Throwable t) {

                            }
                        });

                    }
            }
        });
    }

    private void replace(Fragment fragment){
        transaction=manager.beginTransaction();
        transaction.replace(R.id.clubinfo,fragment);
        transaction.commit();
    }
    public void intvie(){
        sp = getSharedPreferences("login_uid", Context.MODE_PRIVATE);
        userId=sp.getString("userId","");
        sessionId=sp.getString("sessionId","");
        clubId=getIntent().getStringExtra("Club_Id");
        Club_Name_Pic=getIntent().getStringExtra("Club_Pic");
        Club_Name_xml=getIntent().getStringExtra("Club_Name");
        guanzhu= (ImageView)findViewById(R.id.guanzhu);
        radioGroup= (RadioGroup)findViewById(R.id.radioGroup);
        Club_Name= (TextView) findViewById(R.id.club_radio_name);
        Club_Name.setText(Club_Name_xml);
        Club_Pic= (ImageView) findViewById(R.id.club_radio_pic);
        Picasso.with(ClubInfoActivity.this)
                .load(Club_Name_Pic)
                .into(Club_Pic);
        ClubeModel model = new ClubeModel();
        Call<Integer> call=model.getClubState(clubId,userId,"1",sessionId);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
               int res= response.body();
                if(res==0){
                    Toast.makeText(ClubInfoActivity.this, res+"", Toast.LENGTH_SHORT).show();
                    flag=true;
                    guanzhu.setImageResource(R.drawable.ic_guanzhu_hong);
                }else{
                    Toast.makeText(ClubInfoActivity.this, res+"", Toast.LENGTH_SHORT).show();
                    flag=false;
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}
